package com.example.city_home_app.Activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.city_home_app.Fragment.Fragment_FansUser;
import com.example.city_home_app.Fragment.Fragment_FocusPost;
import com.example.city_home_app.Fragment.Fragment_FocusUser;
import com.example.city_home_app.Fragment.Fragment_LikeMoment;
import com.example.city_home_app.Fragment.Fragment_LikePost;
import com.example.city_home_app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyLikeActivity extends BaseActivity {
    @BindView(R.id.toolbar_mylike)
    Toolbar toolbarMylike;
    @BindView(R.id.tv_mylike_post)
    TextView tvMylikePost;
    @BindView(R.id.tv_mylike_moment)
    TextView tvMylikeMoment;
    @BindView(R.id.mylike_vp_content)
    FrameLayout mylikeVpContent;

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private Fragment fragment_post, fragment_moment;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_mylike;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        showToast();
        Window window = MyLikeActivity.this.getWindow();
        //如果系统5.0以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(0xff49afcd);
        }
    }

    @Override
    protected void initData() {
        super.initData();
        selectPage(0);
    }

    private void selectPage(int select) {
        resetView();
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (select) {
            case 0:
                if (fragment_post == null) {
                    fragment_post = new Fragment_LikePost();
                    fragmentTransaction.add(R.id.mylike_vp_content, fragment_post);
                } else {
                    fragmentTransaction.show(fragment_post);
                }
                tvMylikePost.setTextColor(0xff49afcd);
                break;
            case 1:
                if (fragment_moment == null) {
                    fragment_moment = new Fragment_LikeMoment();
                    fragmentTransaction.add(R.id.mylike_vp_content, fragment_moment);
                } else {
                    fragmentTransaction.show(fragment_moment);
                }
                tvMylikeMoment.setTextColor(0xff49afcd);
                break;
        }
        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (fragment_post != null) {
            fragmentTransaction.hide(fragment_post);
        }
        if (fragment_moment != null) {
            fragmentTransaction.hide(fragment_moment);
        }
    }

    private void resetView() {
        tvMylikePost.setTextColor(0xff929292);
        tvMylikeMoment.setTextColor(0xff929292);
    }


    @OnClick({R.id.tv_mylike_post, R.id.tv_mylike_moment})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_mylike_post:
                selectPage(0);
                break;
            case R.id.tv_mylike_moment:
                selectPage(1);
                break;
        }
    }

    private void showToast() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        toolbarMylike = (Toolbar) findViewById(R.id.toolbar_mylike);
        setSupportActionBar(toolbarMylike);
        toolbarMylike.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}
